package com.dtf.admin.staff.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dtf.admin.staff.form.LoginForm;
import com.dtf.admin.staff.service.LoginService;
import com.dtf.admin.staff.vo.SystemStaff;
import com.dtf.core.common.utils.Contanst;


@Controller
@RequestMapping("/admin/")
public class LoginController {
	
	@Resource
	private LoginService loginService;

	/**
	 * @Title: login
	 * @Description: 登录方法
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @param form
	 * @throws
	 * @author WYF
	 * @Date 2014年12月6日 下午9:46:05
	 */
	@RequestMapping("login")
	@ResponseBody
	public Map login(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, LoginForm form) {
		
			SystemStaff systemStaff = loginService.login(
					form.getLoginname(), form.getPassword());
			
			HashMap reqMap = new HashMap();
			
			if (systemStaff == null) {
				reqMap.put("msg","登录账户不存在或者当前密码不匹配");
				reqMap.put("code", "100");
			}else{
				reqMap.put("code", "0");
				request.getSession().setAttribute(Contanst.CURRENT_USER_KEY,systemStaff);
				loadMenuToSession(request, systemStaff);
			}
			return reqMap;
	}
	
	/**
	 * 加载权限内的菜单列表
	 * @param request
	 * @param systemStaff
	 */
	public void loadMenuToSession(HttpServletRequest request,SystemStaff systemStaff){
		List menus = loginService.loadMenu(systemStaff);
		request.getSession().setAttribute(Contanst.CURRENT_MENU_KEY, menus);
	}
	
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @param form
	 * @return
	 */
	@RequestMapping("loginOut")
	@ResponseBody
	public Map loginOut(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, LoginForm form){
		request.getSession().removeAttribute(Contanst.CURRENT_USER_KEY);
		request.getSession().removeAttribute(Contanst.CURRENT_MENU_KEY);
		Map req = new HashMap();
		req.put("outurl", "/admin/login.jsp");
		return req;
	}
}
